package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private MongoTemplate template;

    @Autowired
    private DestinationRepository repository;

    @Autowired
    private RegionRepository rRepository;

    @Override
    public List<Destination> list() {
         List<Destination> destinations = template.findAll(Destination.class);
        return destinations;
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }


    @Override
    public List<Destination> getDestByRegionId(String rid) {

        Region region = rRepository.findById(rid).orElse(null);

        List<String> refIds = region.getRefIds();

        List<Destination> destinations = repository.findByIdIn(refIds);

        return destinations;
    }

    @Override
    public List<Destination> queryDestByRegionId(String regionId) {
        List<Destination> destinations = null;
        if (("-1").equals(regionId)) {
            destinations = repository.findByParentName("中国");
        }else {
            destinations = this.getDestByRegionId(regionId);
        }

        // 儿子太多,现在只想取五个
        Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");

        // 查询目的地下的目的地
        for (Destination destination : destinations) {
            List<Destination> childrens = repository.findByParentId(destination.getId(),pageable);
                    destination.setChildren(childrens);
        }
        return destinations;
    }

    @Override
    public List<Destination> getDestByDeep(Long deep) {

        List<Destination> destinations = repository.findByDeep(deep);

        return destinations;
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {

        // 1. 查询对象
        Query query = new Query();

        // 2. 封装参数
        // 2.1 高级查询条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        // 2.2 总条数
        long count = template.count(query, Destination.class);
        if (count ==0){ //当总条数等于零时
            return Page.empty();
        }
        // 2.3 分页信息
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        // 3.执行查询语句
        List<Destination> destinations = template.find(query, Destination.class);

        return new PageImpl<>(destinations,pageable,count);
    }

    @Override
    public List<Destination> queryToasts(String destId) {
        List<Destination> list = new ArrayList<>();
        while (StringUtils.hasLength(destId)){
            Destination destination = repository.findById(destId).orElse(null);
            list.add(destination);
            destId = destination.getParentId();
        }
        // 集合元素顺序调转
        Collections.reverse(list);
        return list;
    }

    @Override
    public void updateInfo(String id, String info) {
        Destination destination = repository.findById(id).orElse(null);
        destination.setInfo(info);
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public List<Destination> search(String regionId) {
        List<Destination> destinations = this.queryDestByRegionId(regionId);
        return destinations;
    }

}
